
#include "stm32f10x_it.h"
#include "bsp_clkconfig.h"

#include "bsp_nvic.h"

#include "bsp_global.h"

#include "GPIO/bsp_gpio_config.h"

#include "UART/bsp_uart.h"
#include "IIC/bsp_iic.h"
#include "SPI/spi_flash.h"

#include "bsp_dma.h"
#include "Device/ssd1306.h"
#include "Device/w25q64.h"

#define SEND_TO(x) USART_SendData(USART1, usartData); \
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); \
GPIO_ResetBits(GPIOA, x); \
SysTickInit(waitms); \
GPIO_SetBits(GPIOA, x); 

#define BEEP_ON GPIO_SetBits(GPIOB, GPIO_Pin_0);
#define BEEP_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_0);

#define LED1_ON GPIO_ResetBits(GPIOA, GPIO_Pin_1);
#define LED2_ON GPIO_ResetBits(GPIOA, GPIO_Pin_2);
#define LED3_ON GPIO_ResetBits(GPIOA, GPIO_Pin_3);

#define LED1_OFF GPIO_SetBits(GPIOA, GPIO_Pin_1); // LED1 ��ɫ ��
#define LED2_OFF GPIO_SetBits(GPIOA, GPIO_Pin_2); // LED2 ��ɫ ��
#define LED3_OFF GPIO_SetBits(GPIOA, GPIO_Pin_3); // LED3 ��ɫ ��

void initConfig()
{
	// ��ʼ��GPIO
	GPIO_Config();
	
	EXTI_KEY_Config();
	// UART
	USART_Config();
	// DMA
	DMA_Config();
	// IIC
	IIC_Config();
	// SPI FLASH
	SPI_FLASH_Config();
	/*---------------------------------Device----------------*/
	// SSD1306 SCREEN
	SSD1306_Init();
}

int main(void)
{	
	uint32_t waitms, waitms2;
	uint16_t keyStatus;
	uint16_t usartData;
	
	waitms = 300;
	waitms2 = 100;
	usartData = 0x0100;
	
	char str[50];
	str[49] = '\0';
	
	uint8_t Buffer[500] = {0};
	setBuffer(Buffer);
	
	initConfig();
	// �����ӭ���
	Usart_SendString(USART1, "��ӭʹ�ÿ�����\n");
	// ��ʾ��������
	SSD1306_WriteString("Hello World To My STM32", Font_7x10, 1);
	SysTickInit(1000);
	
	sprintf(str, "Data:0x%x", SPI_FLASH_Read_JEDEC_ID());
	SSD1306_WriteString(str, Font_7x10, 1);
	
	char read_buffer[21];
	read_buffer[20] = '\0';

	char write_buffer[21];
	write_buffer[20] = '\0';
	
	float write_float = 1.38450f;
	float read_float = 0.0f;
	
	// sprintf(write_buffer, "TestData");
	
	SPI_FLASH_Write_Buffer(0, (uint8_t*)(&write_float), sizeof(write_float));
	while(1){
		// �жϰ�ť
		keyStatus = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
		if(0 != keyStatus){ // ���°�ť
			BEEP_ON
			LED1_ON
			SysTickInit(waitms);
			BEEP_OFF
			LED2_ON
			SysTickInit(waitms);
			BEEP_ON
			LED3_ON
			SysTickInit(waitms);
			
			SPI_FLASH_Read_Buffer(0, (uint8_t*)(&read_float), sizeof(read_float));
			sprintf(str, "Data:%lf", read_float);
			SSD1306_WriteString(str, Font_7x10, 1);
			
			BEEP_OFF
			LED1_OFF
			SysTickInit(waitms);
			BEEP_ON
			LED2_OFF
			SysTickInit(waitms);
			BEEP_OFF
			LED3_OFF
		}else if(0 == keyStatus){ // ̧��ť
			BEEP_OFF
			LED1_OFF
			LED2_OFF
			LED3_OFF
			
			// sprintf(str, "Data:0x%x", SPI_FLASH_Read_JEDEC_ID());
			// SSD1306_WriteString(str, Font_7x10, 1);
		}
		

		// ����ͨ��
		if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){
			// ����δ�յ���Ϣ
			continue;
		}
		// �����յ���Ϣ����һ��
		BEEP_ON
		SysTickInit(waitms);
		BEEP_OFF
		usartData = USART_ReceiveData(USART1);
		
		if((uint16_t)0x01 == usartData){ // �յ�0x1
			SEND_TO(GPIO_Pin_1); // ��Ϣ����ȥLED1��һ��
		}else if((uint16_t)0x02 == usartData){ // �յ�0x2
			SEND_TO(GPIO_Pin_2); // ��Ϣ����ȥLED2��һ��
		}else if((uint16_t)0x03 == usartData){ // �յ�0x3
			SEND_TO(GPIO_Pin_3); // ��Ϣ����ȥLED3��һ��
		}else{
			USART_SendData(USART1, usartData);
			while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
			// ������3��
			BEEP_ON
			SysTickInit(waitms2);
			BEEP_OFF
			SysTickInit(waitms2);
			BEEP_ON
			SysTickInit(waitms2);
			BEEP_OFF
			SysTickInit(waitms2);
			BEEP_ON
			SysTickInit(waitms2);
			BEEP_OFF
		}

		USART_ClearITPendingBit(USART1, USART_IT_RXNE); // �ֶ���һ��
		usartData = (uint16_t)0x00;
	}
	return 0;
}